package com.zealcomm.ivcscustomer.service

import android.os.Bundle
import android.view.View
import com.zealcomm.annotations.SystemTraceType
import com.zco.util.listener.CustomOnClickListener
import com.zco.util.widget.MyProgressDialog
import com.zealcomm.commonui.base.BaseFragment2
import com.zealcomm.commonui.beans.SystemTraceBean
import com.zco.ivcs.customer.IvcsCustomerCallback
import com.zco.ivcs.customer.base.entity.BusinessScenariosBean
import com.zealcomm.ivcscustomer.R
import com.zealcomm.ivcscustomer.app.IvcsApplication
import com.zealcomm.ivcscustomer.databinding.FragmentServiceListBinding
import com.zealcomm.ivcscustomer.utils.SystemTraceUtil
import com.zealcomm.utils.bindView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 呼叫客服界面
 */
class ServiceListFragment() : BaseFragment2(R.layout.fragment_service_list) {

    constructor(showProgress: Boolean) : this() {
        this.showProgress = showProgress
    }

    private val binding: FragmentServiceListBinding by bindView()
    private var showProgress = true
    var selectBusinessScenario: BusinessScenariosBean.BusinessScenario? = null
        set(value) {
            field = value
            binding?.btnCustomerTypeChoice?.text = value?.name
        }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        IvcsApplication.ivcsCustomer?.setPublishInterceptor(null)
        if (!showProgress) {
            binding?.clProgress?.visibility = View.GONE
        }
        val callOnClickListener = object : CustomOnClickListener<Any>() {
            override fun onCustomClick(view: View?) {
                var onlyAudio = false
                if (view?.id == R.id.btn_only_audio){
                    onlyAudio = true
                }
                if (selectBusinessScenario == null) {
                    showToast("请先选择业务类型")
                    return
                }
                MyProgressDialog.show(holdingActivity)
                var formId = ""
                if (holdingActivity is MainActivity) {
                    (holdingActivity as MainActivity).formId.let {
                        if (it != -1) {
                            formId = it.toString()
                        }
                    }
                    (holdingActivity as MainActivity).onlyAudio = onlyAudio
                }
                IvcsApplication.callAgent(
                    selectBusinessScenario!!.id,
                    formId,
                    onlyAudio ,
                    object : IvcsCustomerCallback {
                        override fun onSuccess() {
                            GlobalScope.launch(Dispatchers.Main) {
                                MyProgressDialog.dismissProgress()
                            }
                            val info = SystemTraceBean.Info()
                            if (IvcsApplication.customerTokenBody!!.data.profile.fullName == null) {
                                info.applicationId = -1
                            }
                            SystemTraceUtil.addTrace(
                                SystemTraceType.SEND_APPLICATION,
                                info
                            )
                            if (holdingActivity is MainActivity) {
                                (holdingActivity as MainActivity).showRoom()
                            }
                        }

                        override fun onFailed(code: Int, error: String) {
                            GlobalScope.launch(Dispatchers.Main) {
                                MyProgressDialog.dismissProgress()
                            }
                            showToast(error)
                        }
                    })
            }
        }
        binding?.btnLogin?.setOnClickListener(callOnClickListener)
        binding?.btnOnlyAudio?.setOnClickListener(callOnClickListener)
        binding?.btnCustomerTypeChoice?.setOnClickListener(object : CustomOnClickListener<Any>() {
            override fun onCustomClick(view: View?) {
                if (holdingActivity is MainActivity) {
                    (holdingActivity as MainActivity).showCustomerTypeList()
                }
            }
        })
        binding?.btnVideoSettings?.setOnClickListener(object : CustomOnClickListener<Any>() {
            override fun onCustomClick(view: View?) {
                if (holdingActivity is MainActivity) {
                    (holdingActivity as MainActivity).showVideoSettings()
                }
            }
        })
    }

}